/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import BLL.OrderBLL;
import DAL.OrdersDAL;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import xmlobject.DTO.OrderCheckout.Products;
import xmlobject.DTO.Orders.Order;

/**
 *
 * @author kenisk
 */
public class CheckOutController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            if (action.equals("CheckOut")) {
                String Name = request.getParameter("NameReceiver");
                String Address1 = request.getParameter("Address1");
                String Address2 = request.getParameter("Address2");
                String Phone = request.getParameter("Phone");
                Order order = new Order();
                order.setReciverName(Name);
                order.setAddress1(Address1);
                order.setAddress2(Address2);
                order.setPhoneNumber(Phone);
                Writer writer = new StringWriter();
                char[] buffer = new char[1024];
                Reader reader = new BufferedReader(
                        new InputStreamReader(request.getInputStream(), "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
                String orderContentString = writer.toString();
                String realpath = getServletContext().getRealPath("/");
                JAXBContext jc = JAXBContext.newInstance("xmlobject.DTO.OrderCheckout");
                Unmarshaller u = jc.createUnmarshaller();
                SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = sf.newSchema(new File(realpath + "xsd/OrderCheckout.xsd"));
                u.setSchema(schema);
                Products products = (Products) u.unmarshal(new InputSource(new StringReader(orderContentString)));
                System.out.println(products.getProduct().get(0).getProductID());
                OrdersDAL orderDAL = new OrdersDAL();
                int orderID = orderDAL.addNewOrder(products, order);
                response.setContentType("text/text-plain;charset=UTF-8");
                out.write(orderID+"");
            }
        } catch (SAXException ex) {
            Logger.getLogger(CheckOutController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(CheckOutController.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
